package com.cenntro.common.util.image;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageProducer;
import java.io.File;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import com.sun.jimi.core.Jimi;
import com.sun.jimi.core.JimiException;
import com.sun.jimi.core.JimiWriter;
import com.sun.jimi.core.options.JPGOptions;

public class TransferPicture {

	/**
	 * @param source
	 * @param dest
	 * @param quality
	 *            图片格式转换
	 */
	public static void toJPG(String source, String targert, int quality) {
		// 0<quality<100
		if (quality < 0 || quality > 100 || (quality + "") == null || (quality + "").equals("")) {
			System.out.println("quality must between ’0’ and ’100’");
			System.out.println("set to DEFAULT value:’75’");
			quality = 75;
		}
//		String[] str = StringUtils.split(source, ".");
		String outfile = targert;
		try {
			JPGOptions options = new JPGOptions();
			options.setQuality(quality);
			ImageProducer image = Jimi.getImageProducer(source);
			JimiWriter writer = Jimi.createJimiWriter(outfile);
			writer.setSource(image);
			// 加入属性设置，非必要
			writer.setOptions(options);
			writer.putImage(outfile);
		} catch (JimiException je) {
			System.err.println("Error: " + je);
		}
	}

	/**
	 * @param source
	 * @param dest
	 * @throws JimiException
	 */
	public static void toGIF(String source, String dest) throws JimiException {
		if (dest == null || dest.trim().equals(""))
			dest = source;
		// 1:转换为jpg
		if (!dest.toLowerCase().trim().endsWith("jpg")) {
			dest += ".jpg";
		}
		toJPG(source, dest, 75);
		BufferedImage file_in = null;
		File file = new File(dest);
		try {
			file_in = javax.imageio.ImageIO.read(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		int end = dest.lastIndexOf(".");
		file.deleteOnExit();
		// output *.gif
		file.renameTo(new File(dest.substring(0, end) + ".gif"));
		// jpg to gif
		AnimatedGifEncoder e = new AnimatedGifEncoder();
		e.start(dest);
		e.addFrame(file_in);
		e.finish();
	}

	/**
	 * @param img
	 * @param dest
	 * @throws JimiException
	 */
	public static void toTIF(Image img, String dest) throws JimiException {
		if (!dest.toLowerCase().trim().endsWith("tif")) {
			dest += ".tif";
			System.out.println("Overriding to TIF, output file: " + dest);
		}
		dest = dest.substring(0, dest.lastIndexOf(".")) + ".jpg";
		try {
			System.out.println("toTIF encode");
			JimiWriter writer = Jimi.createJimiWriter(dest);
			writer.setSource(img);
			dest = dest.substring(0, dest.lastIndexOf(".")) + ".tif";
			writer.putImage(dest);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 线性改变图片尺寸(可同时改变图片格式)
	 * 
	 * @param source
	 *            源文件完整路径
	 * @param desc
	 *            目标文件完整路径
	 * @param ins
	 *            放大/缩小比率
	 * @throws JimiException
	 * @throws IOException
	 */
	public static void changeDimension(String source, String dest, double ins) throws JimiException, IOException {
		String temp = dest;
		File _file = null;
		if (dest == null || dest.trim().equals(""))
			dest = source;
		if (!dest.toLowerCase().trim().endsWith("jpg")) {
			temp = dest.trim() + ".jpg";
		}
		toJPG(source, temp, 75);
		_file = new File(temp); // 读入文件
		Image src = javax.imageio.ImageIO.read(_file); // 构造Image对象
		double wideth = (double) src.getWidth(null); // 得到源图宽
		double height = (double) src.getHeight(null); // 得到源图长
		System.out.println("源图宽:" + wideth);
		System.out.println("源图长:" + height);
		// 缩放处理
		int iWideth = (int) (wideth * ins);
		int iHeight = (int) (height * ins);
		System.out.println("现图宽:" + iWideth);
		System.out.println("现图长:" + iHeight);
		System.out.println("缩放参数:" + ins);
		BufferedImage tag = null;
		try {
			tag = new BufferedImage(iWideth, iHeight, BufferedImage.TYPE_INT_RGB);
			tag.getGraphics().drawImage(src, 0, 0, iWideth, iHeight, null); // 绘制缩小后的图
			/*
			 * //打水印 File _filebiao = new File("test2.jpg"); Image src_biao =
			 * ImageIO.read(_filebiao); int wideth_biao = 40;//src_biao.getWidth(null); int
			 * height_biao = 40;//src_biao.getHeight(null);
			 * tag.getGraphics().drawImage(src_biao,50,50,wideth_biao, height_biao,null);
			 */
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!temp.trim().equals(dest))
			_file.deleteOnExit();
		if (dest.toLowerCase().trim().endsWith("gif")) {
			System.out.println("the type is gif!");
			AnimatedGifEncoder e = new AnimatedGifEncoder();
			e.start(dest);
			e.addFrame(tag);
			e.finish();
		} else if (dest.toLowerCase().trim().endsWith("tif") || dest.toLowerCase().trim().endsWith("tiff")) {
			System.out.println("the type is tif!");
			toTIF(tag, dest);
		} else {
			try {
				System.out.println("common type!");
				JimiWriter writer = Jimi.createJimiWriter(dest);
				writer.setSource(tag);
				writer.putImage(dest);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	public static void transpic(String source, int iWideth, int iHeight, String dest)
			throws JimiException, IOException {
		String temp = dest;
		File _file = null;
		if (dest == null || dest.trim().equals(""))
			dest = source;
		if (!dest.toLowerCase().trim().endsWith("jpg")) {
			temp = dest.trim() + ".jpg";
		}
		toJPG(source, temp, 75);
		_file = new File(temp); // 读入文件
		Image src = javax.imageio.ImageIO.read(_file); // 构造Image对象
		double wideth = (double) src.getWidth(null); // 得到源图宽
		double height = (double) src.getHeight(null); // 得到源图长
		System.out.println("源图宽:" + wideth);
		System.out.println("源图长:" + height);
		System.out.println("现图宽:" + iWideth);
		System.out.println("现图长:" + iHeight);
		// 缩放处理
		BufferedImage tag = null;
		try {
			tag = new BufferedImage(iWideth, iHeight, BufferedImage.TYPE_INT_RGB);
			tag.getGraphics().drawImage(src, 0, 0, iWideth, iHeight, null); // 绘制缩小后的图
			/*
			 * //打水印 File _filebiao = new File("test2.jpg"); Image src_biao =
			 * ImageIO.read(_filebiao); int wideth_biao = 40;//src_biao.getWidth(null); int
			 * height_biao = 40;//src_biao.getHeight(null);
			 * tag.getGraphics().drawImage(src_biao,50,50,wideth_biao, height_biao,null);
			 */
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!temp.trim().equals(dest))
			_file.deleteOnExit();
		if (dest.toLowerCase().trim().endsWith("gif")) {
			System.out.println("the type is gif!");
			AnimatedGifEncoder e = new AnimatedGifEncoder();
			e.start(dest);
			e.addFrame(tag);
			e.finish();
		} else if (dest.toLowerCase().trim().endsWith("tif") || dest.toLowerCase().trim().endsWith("tiff")) {
			System.out.println("the type is tif!");
			toTIF(tag, dest);
		} else {
			try {
				System.out.println("common type!");
				JimiWriter writer = Jimi.createJimiWriter(dest);
				writer.setSource(tag);
				writer.putImage(dest);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		String dest = "C:/Users/Administrator/Pictures/test.jpg";
		String source = "C:/Users/Administrator/Pictures/QQ截图20171228210216.png";
		int quality = 100;
		double ins = 4;
		// TransferPicture tp = new TransferPicture();
		try {
			System.out.println("before change");
			System.out.println("-------------------");
			// tp.toJPG(source,dest,quality);
			// tp.toGIF(source, dest);
//			TransferPicture.transpic(source, 200,200, dest);
			TransferPicture.changeDimension(source, dest, 0.5);
			System.out.println("-------------------");
			System.out.println("end change");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}